package com.lostandfound.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lostandfound.entity.Reply;
import com.lostandfound.entity.User;
import com.lostandfound.service.ReplyService;
import com.lostandfound.util.ReturnCode;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("basePackage")
@Namespace("/")
@Results({  
    @Result(type = "json", params = {"root", "res"})  
}) 
public class ReplyAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private ReplyService replyService;
	
	private Reply reply;//对象模型
	private Map<String, Object> res = new HashMap<String, Object>();//返回json信息
	private HashMap<String, Object> pagemap;//分页信息
	
	public Reply getReply() {
		return reply;
	}
	public void setReply(Reply reply) {
		this.reply = reply;
	}
	
	public Map<String, Object> getRes() {
		return res;
	}
	public void setRes(Map<String, Object> res) {
		this.res = res;
	}
	
	public HashMap<String, Object> getPagemap() {
		return pagemap;
	}
	public void setPagemap(HashMap<String, Object> pagemap) {
		this.pagemap = pagemap;
	}
	
	/**
	 * 获取session中保存的当前登录用户信息
	 * @return
	 */
	private User getUser(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		User loginUser = (User)session.get("loginUser");
		return loginUser;
	}
	
	/**
	 * 添加回帖
	 * @return
	 */
	@Action(value="saveReply")
	public String saveReply(){
		if(this.reply != null){
			try {
				this.reply.setR_create_time(new Date());
				this.reply.setUser(getUser());
				replyService.save(this.reply);
				res.put("code", ReturnCode.SUCCESS);//添加成功
			} catch (Exception e) {
				System.out.println("**添加回帖失败**");
				res.put("code", ReturnCode.FAIL);
			}
		}else{
			res.put("code", ReturnCode.LOST_NEWS);//添加信息不能为空
		}
		System.out.println("reply");
		return SUCCESS;
	}
	
	/**
	 * 删除一个或者多个回帖信息
	 * @return
	 */
	@Action(value="deleteReply")
	public String deleteReply(){
		if(this.res != null && this.res.get("ids") != null){
			String[] ids = (String[])this.res.get("ids");
		
			String rids = ids[0]; 
			rids = rids.substring(1, rids.length()-1);
			String[] id = rids.split(",");
			if(id != null && id.length >0){
				Long[] r_ids = new Long[id.length];
				for (int i=0;i<id.length;i++) {
					Long l1 = Long.parseLong(id[i]);
					r_ids[i] = l1;
				}
				try {
					replyService.deleteReplyByBranch(r_ids);//删除回帖
					this.res.put("code", ReturnCode.SUCCESS);//删除成功
				} catch (Exception e) {
					System.out.println("**删除回帖信息失败！**");
					this.res.put("code", ReturnCode.FAIL);
				}
			}
		}else{
			this.res.put("code", ReturnCode.LOST_NEWS);//要删除的集合ID不能为空
		}
		return SUCCESS;
	}
	
	/**
	 * 根据ID查询信息
	 * @return
	 */
	@Action(value="selectReplyByPrimary")
	public String selectReplyByPrimary(){
		if(this.reply != null){
			try {
				reply = replyService.selectByPrimary(this.reply.getRid());
				res.put("reply", reply);
				res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("**获取回帖详情失败!**");
				res.put("code", ReturnCode.FAIL);
			}
		}else{
			res.put("code", ReturnCode.LOST_NEWS);//要查询的信息ID不能为空
		}
		return SUCCESS;
	}
	
	/**
	 * 分页获取回复列表
	 * @return
	 */
	@Action(value="listReplyByPage")
	public String listReplyByPage(){
		if(pagemap == null){//pagemap存放分页属性，如果pagemap为空，设置默认每页显示10条数据
			pagemap = new HashMap<String, Object>();
			pagemap.put("page", "1");
			pagemap.put("size", "10");
		}
		try {
			@SuppressWarnings("unchecked")
			List<Reply> replys = (List<Reply>)replyService.listReplyByPage(pagemap);//分页查询板块信息
			int count = replyService.Count(pagemap);
			this.res.put("replys", replys);//获取当页数据
			this.res.put("replycount",count);//获取总条目
			int size = Integer.parseInt((String)pagemap.get("size"));
			this.res.put("totalpage",count%size>0?count/size+1:count/size);//获取总页数
			this.res.put("code", ReturnCode.SUCCESS);//查询成功
		} catch (Exception e) {
			System.out.println("查询失败！");
			this.res.put("code", ReturnCode.FAIL);
		}
		return SUCCESS;
	}
	
	
	
	
	

}
